package com.atguigu.source;

import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.connector.source.util.ratelimit.RateLimiterStrategy;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.connector.datagen.source.DataGeneratorSource;
import org.apache.flink.connector.datagen.source.GeneratorFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * @author yhm
 * @create 2024-04-02 8:50
 */
public class Test05_DataGen {
    public static void main(String[] args) throws Exception {
        // 1. 创建环境
        Configuration conf = new Configuration();
        conf.setInteger("rest.port",8081);

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);

        // 2. 读取数据源
        DataGeneratorSource<String> dataGenSource = new DataGeneratorSource<>(new GeneratorFunction<Long, String>() {
            @Override
            public String map(Long value) throws Exception {
                // value就是一个计数器
                return "hello:" + value;
            }
        }, Integer.MAX_VALUE, RateLimiterStrategy.perSecond(2), Types.STRING);
        DataStreamSource<String> dataStreamSource = env.fromSource(dataGenSource, WatermarkStrategy.noWatermarks(), "gen_source");
        // 3. 处理数据

        // 4. 输出
        dataStreamSource.print();

        // 5. 执行环境
        env.execute();
    }
}
